package com.citycloud.ccuap.tc.admin.projecttype;

import java.util.List;
import java.util.Map;

import com.citycloud.ccuap.commons.sys.entity.SysProject;
import com.citycloud.ccuap.commons.sys.repository.SysProjectRepository;
import com.citycloud.ccuap.tc.admin.project.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.openapi.controller.base.BaseController;
import com.citycloud.ccuap.commons.web.session.CurrentUser;
import com.citycloud.ccuap.commons.web.util.ResolveRequest;
import com.citycloud.ccuap.framework.exception.AppAssert;
import com.citycloud.ccuap.framework.mybatis.pagination.PageQuery;
import com.citycloud.ccuap.tc.admin.entity.SysProjectRelApp;
import com.citycloud.ccuap.tc.admin.entity.SysProjectType;
import com.citycloud.ccuap.tc.admin.projectrelapp.SysProjectRelAppService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

/**
 * @Description
 * @Date 2019/12/19 2:06 PM
 * @Author liuyan
 **/
@Api(tags = "项目（站点）信息")
@RestController("yqProjectTypeController")
@RequestMapping("/v1/yq/project")
public class ProjectTypeController extends BaseController {

    @Autowired
    private IProjectTypeService projectTypeService;
    @Autowired
    private ProjectService projectService;
    @Resource
    private SysProjectRepository sysProjectRepository;

    @ApiOperation("获取站点类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="start",value = "当前页",dataTypeClass = Integer.class,paramType = "query",allowEmptyValue = true,defaultValue = "1"),
            @ApiImplicitParam(name="size",value = "每页显示条数",dataTypeClass = Integer.class,paramType = "query",allowEmptyValue = true,defaultValue = "10"),
            @ApiImplicitParam(name="isGetAll",value = "是否查询所有（最大值Integer.MAX_VALUE）",dataTypeClass = Boolean.class,paramType = "query",allowEmptyValue = true),
            @ApiImplicitParam(name="name",value = "站点名称",dataTypeClass = String.class,paramType = "query",allowEmptyValue = true)
    })
    @RequestMapping(value = "/types", method = {RequestMethod.GET})
    public JSONData getTypes(){
        JSONData jsonData = new JSONData();
        //分页参数
        List<SysProjectType> types = projectTypeService.selectBasicList();
        jsonData.setData(types);
        return jsonData;
    }

    @ApiOperation("分页查询站点列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="start",value = "当前页",dataTypeClass = Integer.class,paramType = "query",allowEmptyValue = true,defaultValue = "1"),
            @ApiImplicitParam(name="size",value = "每页显示条数",dataTypeClass = Integer.class,paramType = "query",allowEmptyValue = true,defaultValue = "10"),
            @ApiImplicitParam(name="isGetAll",value = "是否查询所有（最大值Integer.MAX_VALUE）",dataTypeClass = Boolean.class,paramType = "query",allowEmptyValue = true),
            @ApiImplicitParam(name="name",value = "站点名称",dataTypeClass = String.class,paramType = "query",allowEmptyValue = true)
    })
    @RequestMapping(value = "/types/query", method = {RequestMethod.GET})
    public JSONData getTypesPage() throws Exception{
        //分页参数
        PageQuery<SysProjectType> pageQuery = ResolveRequest.getPageForParams(request);
        SysProjectType sysProjectType = new SysProjectType();
        ResolveRequest.fillData(request, sysProjectType, "", true);
        PageQuery<SysProjectType> typePageQuery = projectTypeService.selectPage(pageQuery, sysProjectType);
        JSONData jsonData  = new JSONData(typePageQuery);
        return jsonData;
    }
    @ApiOperation("获取站点类型详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "站点类型Id",dataTypeClass = String.class,paramType = "query",allowEmptyValue = true),
    })
    @RequestMapping(value = "/types/findById", method = {RequestMethod.GET})
    public JSONData getProjectType(){
        JSONData jsonData = new JSONData();
        String id = getStringParameter("id",null);
        Assert.hasText(id,"站点类型ID不能为空！");
        SysProjectType projectType = projectTypeService.findById(id);
        jsonData.setData(projectType);
        return jsonData;
    }

    @ApiOperation("新增站点类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="projectType",value = "站点类型实体",dataTypeClass = SysProjectType.class,paramType = "body",allowEmptyValue = true),
    })
    @RequestMapping(value = "/types/create", method = {RequestMethod.POST})
    public JSONData createProjectType() throws Exception{
        JSONData jsonData = new JSONData();
        SysProjectType sysProjectType = new SysProjectType();
        ResolveRequest.fillData(request, sysProjectType);
        CurrentUser user = getCurrentUser();
        sysProjectType.setCreator(user == null ? "" : (user.getUser() == null ? "" : String.valueOf(user.getUser().getUserNo())));
        sysProjectType = projectTypeService.create(sysProjectType);
        jsonData.setData(sysProjectType);
        jsonData.setMessage("新建站点类型成功！");
        return jsonData;
    }

    @ApiOperation("修改站点类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="projectType",value = "站点类型实体",dataTypeClass = SysProjectType.class,paramType = "query",allowEmptyValue = true),
    })
    @RequestMapping(value = "/types/modify", method = {RequestMethod.POST})
    public JSONData modifyProjectType() throws Exception{
        JSONData jsonData = new JSONData();
        SysProjectType sysProjectType = new SysProjectType();
        ResolveRequest.fillData(request, sysProjectType);
        AppAssert.hasText(sysProjectType.getId(),"该站点类型不存在！");
        CurrentUser user = getCurrentUser();
        sysProjectType.setUpdator(user == null ? "" : (user.getUser() == null ? "" : String.valueOf(user.getUser().getUserNo())));
        sysProjectType = projectTypeService.modify(sysProjectType);
        jsonData.setData(sysProjectType);
        jsonData.setMessage("修改站点类型成功！");
        return jsonData;
    }

    @ApiOperation("删除站点类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "站点类型Id",dataTypeClass = String.class,paramType = "query",allowEmptyValue = true),
    })
    @RequestMapping(value = "/types/delete", method = {RequestMethod.POST})
    public JSONData deleteProjectType(){
        //id不能为空
        JSONData jsonData = new JSONData("删除站点类型成功！");
        String id = getStringParameter("id",null);
        Assert.hasText(id,"站点类型ID不能为空！");
        //校验是否关联站点，关联提示解绑处理
        List<Map<String,Object>> sysProjects = projectService.findSysProjectByProjectTypeId(id);
        AppAssert.isTrue(CollectionUtils.isEmpty(sysProjects),"请解绑关联站点后，再删除该站点类型！");
        projectTypeService.deleteById(id);
        return jsonData;
    }

}
